package jm.nj.demo18main.demo05;

import jm.nj.demo18main.demo04.User;
import org.apache.commons.beanutils.BeanUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.List;

/*
 * 综合案例:
 * 	使用:xml+dom4j+反射+BeanUtils
 * 	目的:
 * 		写出xml文档存储JavaBean的配置信息
 * 		使用dom4j解析xml文档,获取文档内容
 * 		使用反射技术通过解析获取的全类名创建JavaBean对象
 * 		使用BeanUtils工具类给JavaBean成员变量注入值
 */
public class UserDOM4J {
    public static void main(String[] args) throws Exception {
        SAXReader sax = new SAXReader();
        Document doc = sax.read("data.xml");
        Element rootElement = doc.getRootElement();
        List<Element>beanElementList = rootElement.elements();
        for (Element beanelement : beanElementList) {
            String beanName = beanelement.attributeValue("className");
            //System.out.println(beanName);
            //Class clazz = User.class;
            Class clazz = Class.forName(beanName);
            Object obj = clazz.newInstance();
            List<Element>proElements = beanelement.elements();
            for (Element porElement : proElements) {
                String porname = porElement.attributeValue("name");
                String porvalue = porElement.attributeValue("value");
                //System.out.println(porname);
                //System.out.println(porvalue );
                BeanUtils.setProperty(obj,porname,porvalue);
            }
            System.out.println(obj);



        }


    }

}
